package com.distribute.search.service.impl;

import java.io.IOException;
import java.util.List;

import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.distribute.common.pojo.DistributeResult;
import com.distribute.search.mapper.ItemMapper;
import com.distribute.search.pojo.Item;
import com.distribute.search.service.ItemService;
import com.distribute.common.util.ExceptionUtil;

@Service
public class ItemServiceImpl implements ItemService {

	@Autowired
	private ItemMapper itemMapper;

	@Autowired
	private SolrServer solrServer;

	@Override
	public DistributeResult importAllItems() {
		try {
			System.out.println("----------------"+"importAllItems"+"----------");
			// 查询宠物列表
			List<Item> list = itemMapper.getItemList();
			System.out.println("----------------"+list.size()+"----------");
			// 把宠物信息写入索引库
			for (Item item : list) {
				// 创建一个SolrInputDocument对象
				System.out.println("----------------"+item.getTitle()+"----------");
				SolrInputDocument document = new SolrInputDocument();
				document.setField("id", item.getId());
				document.setField("item_name", item.getName());
				document.setField("item_title", item.getTitle());
				document.setField("item_age", item.getAge());
				document.setField("item_sex", item.getSex());
				document.setField("item_image", item.getImage());
				document.setField("item_category_name", item.getCategory_name());
				document.setField("item_desc", item.getItem_des());
				// 写入索引库
				solrServer.add(document);
			}
			// 提交修改
			solrServer.commit();
		} catch (Exception e) {
			e.printStackTrace();
			return DistributeResult.build(500, ExceptionUtil.getStackTrace(e));
		}
		return DistributeResult.ok();
	}

	@Override
	public DistributeResult importItem(Item item) {
		// TODO Auto-generated method stub
		try {
			// 创建一个SolrInputDocument对象
			SolrInputDocument document = new SolrInputDocument();
			document.setField("id", item.getId());
			document.setField("item_name", item.getName());
			document.setField("item_title", item.getTitle());
			document.setField("item_age", item.getAge());
			document.setField("item_sex", item.getSex());
			document.setField("item_image", item.getImage());
			document.setField("item_category_name", item.getCategory_name());
			document.setField("item_desc", item.getItem_des());
			// 写入索引库
			solrServer.add(document);
			solrServer.commit();
		} catch (SolrServerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return DistributeResult.ok();
	}

	@Override
	public DistributeResult updateItem(Item item) {
		try {
			// 创建一个SolrInputDocument对象
			SolrInputDocument document = new SolrInputDocument();
			document.setField("id", item.getId());
			document.setField("item_name", item.getName());
			document.setField("item_title", item.getTitle());
			document.setField("item_age", item.getAge());
			document.setField("item_sex", item.getSex());
			document.setField("item_image", item.getImage());
			document.setField("item_category_name", item.getCategory_name());
			document.setField("item_desc", item.getItem_des());
			// 写入索引库
			solrServer.add(document);
			solrServer.commit();
		} catch (SolrServerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return DistributeResult.ok();
	}

	@Override
	public DistributeResult deleteItem(String id) {
		try {
			solrServer.deleteById(id);
			solrServer.commit();
		} catch (SolrServerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

}
